﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TestMatrix.BLController;
using TestMatrix.Helper;
using TestMatrix.Model;
using Visifire.Charts;

namespace TestMatrix.Phone
{
    public partial class MatrixDetailsPage : PhoneApplicationPage
    {

        ApplicationBarController appbar;

        FilterSettingController filterSettingController;

        public MatrixDetailsPage()
        {
            InitializeComponent();

            InitializeDataFilter();
            InitializeControls();
            InitializeControlEvents();

            //DetailsPivot.Title = filterSettingController.DisplayRefreshDuration();
        }

        void InitializeDataFilter()
        {
            try
            {
                string dateFilterType = ChartController.ReadSettingFromXml();
                filterSettingController = new FilterSettingController(dateFilterType);
            }
            catch
            {
            }
        }

        void InitializeControls()
        {
            this.appbar = new ApplicationBarController(this.ApplicationBar);

            //prepare all icon buttons and menu Items
            this.appbar.AddNewButton("project menu", "/icons/appbar.favs.rest.png", true, ApplicationBarIconButton_Click);
            this.appbar.AddNewButton("setting", "/icons/appbar.edit.rest.png", true, ApplicationBarIconButton_Click);
            this.appbar.AddNewButton("refresh", "/icons/appbar.refresh.rest.png", true, ApplicationBarIconButton_Click);
            this.appbar.AddNewButton("save", "/icons/appbar.save.rest.png", true, ApplicationBarIconButton_Click);
            this.appbar.AddNewButton("cancel", "/icons/appbar.close.rest.png", true, ApplicationBarIconButton_Click);
            this.appbar.AddNewButton("back", "/icons/appbar.back.rest.png", true, ApplicationBarIconButton_Click);
            this.appbar.AddNewButton("stop", "/icons/appbar.transport.pause.rest.png", true, ApplicationBarIconButton_Click);
        }

        void InitializeControlEvents()
        {
            DetailsPivot.SelectionChanged += new SelectionChangedEventHandler(DetailsPivot_SelectionChanged);
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            ApplicationBarIconButton btn = (ApplicationBarIconButton)sender;
            string text = btn.Text.ToLower();

            if (text == "setting")
            {
                MessageBox.Show("TODO setting");
                //this.appbar.ShowButtons("Save", "Cancel");
            }
            else if (text == "refresh")
            {
                MessageBox.Show("TODO Refresh");
            }
            else if (text == "back")
            {
                //NavigationService.GoBack();
                NavigationService.Navigate(new Uri("/MatrixSummaryPage.xaml", UriKind.Relative));
            }
        }

        void DetailsPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int i = DetailsPivot.SelectedIndex;

            switch (i)
            {
                case 0:
                    this.appbar.ShowAppBar(true);
                    this.appbar.ShowButtons("back");

                    break;
                case 1:

                    this.appbar.ShowAppBar(true);
                    this.appbar.ShowButtons("back");

                    break;
                //case 2:
                //    this.appbar.ShowAppBar(true);
                //    this.appbar.ShowButtons("Project Menu", "Setting", "Refresh");

                //    break;
                //case 3:
                //    this.appbar.ShowAppBar(true);
                //    this.appbar.ShowButtons("Project Menu", "Setting", "Refresh");

                //    break;
                //case 4:
                //    this.appbar.ShowAppBar(true);
                //    this.appbar.ShowButtons("Project Menu", "Setting", "Refresh");

                //    break;
                //case 5:
                //    this.appbar.ShowAppBar(true);
                //    this.appbar.ShowButtons("Project Menu", "Setting", "Refresh");

                //    break;
                //case 6:
                //    this.appbar.ShowAppBar(true);
                //    this.appbar.ShowButtons("Project Menu", "Setting", "Refresh");

                //    break;
                //case 7:
                //    this.appbar.ShowAppBar(true);
                //    this.appbar.ShowButtons("Project Menu", "Setting", "Refresh");

                //    break;

                //case 8:
                //    this.appbar.ShowAppBar(true);
                //    this.appbar.ShowButtons("Project Menu", "Setting", "Refresh");
                //    //this.appbar.ShowMenuItem("Message");

                //    break;
            }
        }

        void InitializeChart(string inputStr)
        {
            DetailsPivot.Title = filterSettingController.DisplayRefreshDuration()+Environment.NewLine+""+inputStr;

            displayPieData(RenderAs.Pie, inputStr);
            displayFunnelBugState(RenderAs.Pyramid, inputStr);

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            base.OnNavigatedTo(e);
            string prjName = "";

            if (!e.NavigationMode.Equals(NavigationMode.Back) & NavigationContext.QueryString.TryGetValue("PrjName", out prjName))
            {
                prjName = string.Format(prjName);
                InitializeChart(prjName);
            }

        }

        private void displayPieData(RenderAs ra, string str)
        {

            string sql = "select BugType,count(*) as Count from tblBugList where 1=1 and ProjectFriendName=? group by BugType order by Count desc";
            List<tblBugList> listDatas = ChartController.Query(filterSettingController.AppendDateFilter(sql), str);

            DataSeries dataSeries = new DataSeries();
            dataSeries.RenderAs = ra;
            dataSeries.LabelEnabled = true;
            dataSeries.ShowInLegend = true;
            dataSeries.IncludePercentageInLegend = true;

            DataPoint dp;
            for (int i = 0; i < listDatas.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDatas[i].BugType;
                dp.YValue = listDatas[i].Count;
                dp.Exploded = true;
                dataSeries.DataPoints.Add(dp);
            }

            PieBugTypeChart.View3D = true;
            PieBugTypeChart.Series.Add(dataSeries);
        }

        /// <summary>
        /// This is to show Funnel Bug state
        /// </summary>
        /// <param name="ra"></param>
        /// <param name="str"></param>
        private void displayFunnelBugState(RenderAs ra, string str)
        {

            string sql = "select State,count(*) as Count from tblBugList where 1=1 and ProjectFriendName=? group by State order by Count desc";
            List<tblBugList> listDatas = ChartController.Query(filterSettingController.AppendDateFilter(sql), str);

            DataSeries dataSeries = new DataSeries();
            dataSeries.RenderAs = ra;
            //dataSeries.LabelEnabled = true;
            dataSeries.ShowInLegend = false;
            dataSeries.LabelText = "#AxisXLabel, #Percentage%";

            DataPoint dp;
            for (int i = 0; i < listDatas.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDatas[i].State;
                dp.YValue = listDatas[i].Count;
                dp.Exploded = true;
                dataSeries.DataPoints.Add(dp);
            }

            FunnelStateChart.View3D = true;
            FunnelStateChart.Series.Add(dataSeries);
        }
    }
}